import Vue from 'vue'
import VueRouter from 'vuex'
import router,{resetRouter} from '../router/index'
Vue.use(VueRouter)


function addNewRouter(menuList) {
    console.log(menuList)
  let routes= router.options.routes
    console.log(routes) //打印路由
//    遍历路由，找到path:'/Index'
    routes.forEach(routeItem=>{
            if(routeItem.path=="/Index"){
                    menuList.forEach(menu=>{
                        let ChildRoute={
                                path:'/'+menu.menuclick,
                                name:menu.menuname,
                                meta:{
                                    title: menu.menunamed,
                                },
                                component:()=>import('../components/'+menu.menucomponent)
                        };
                        routeItem.children.push(ChildRoute)
                  })
            }
    })
    resetRouter()
    router.addRoutes(routes)

}

export default new VueRouter.Store({
    state:{
        menu:[],
    },
    mutations:{
        setMenu(state,menuList){
            state.menu=menuList;

            addNewRouter(menuList)
        },
    },
    actions:{},
    getters:{
        getMenu(state){
            return state.menu
        },
    },
})